package com.kma.summer2012.client.presenters.reject;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.kma.summer2012.client.CrumbsEventBus;
import com.kma.summer2012.client.events.reject.SuccessfulRejectEvent;
import com.kma.summer2012.client.events.request.MainPageEvent;
import com.kma.summer2012.client.presenters.Presenter;
import com.kma.summer2012.client.presenters.header.HeaderPresenter;
import com.kma.summer2012.client.services.AbstractCallBack;
import com.kma.summer2012.client.services.request.CrumbsRequestService;
import com.kma.summer2012.client.services.session.CrumbsSessionService;
import com.kma.summer2012.shared.model.UICrumbsRequest;


/**
 * Created by IntelliJ IDEA.
 * User: HP
 * Date: 05.02.13
 * Time: 20:37
 * To change this template use File | Settings | File Templates.
 */
public class RejectPresenter implements Presenter {

    public interface Display {
        HasClickHandlers getrejectButton();

        HasClickHandlers getcancelButton();

        String getrichText();

        Widget asWidget();

    }


    private final Display display;
    private UICrumbsRequest request;

    public RejectPresenter(Display display) {
        this.display = display;
        bind();
        CrumbsSessionService.App.getInstance().getParameterRequest("rejectRequest", new AbstractCallBack<UICrumbsRequest>() {
            @Override
            public void onSuccess(UICrumbsRequest uiCrumbsRequest) {
                RejectPresenter.this.request = uiCrumbsRequest;
            }
        });


    }

    private void bind() {
        this.display.getrejectButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                doSaveComments();
            }
        });


        this.display.getcancelButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                CrumbsEventBus.getInstance().fireEvent(new MainPageEvent());

            }
        });

    }

    @Override
    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());
    }

    public void doSaveComments() {
        if (this.display.getrichText().equals("")){
            Window.alert("Please, add some comments");
            return;
        }

        CrumbsRequestService.App.getInstance().sendchangeState(this.request.getId(), this.display.getrichText(), HeaderPresenter.getUser().getUserId(), new AbstractCallBack<Boolean>() {
            @Override
            public void onSuccess(Boolean boo) {
                CrumbsEventBus.getInstance().fireEvent(new SuccessfulRejectEvent());
            }
        });
    }
}
